<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <!-- 
    clearInterval清除定时器
  -->
  <button class="begin">开启定时器</button>
  <button class="end">关闭定时器</button>
  <script>
    let begin = document.querySelector(".begin");
    let end = document.querySelector(".end");
    let setId = null; //null表示空对象
    begin.addEventListener("click", () => {
      setId = window.setInterval(() => {
        console.log(`你好呀了`);
      }, 100);
    });
    end.addEventListener("click", () => {
      clearInterval(setId);
    });
  </script>
</body>

</html>